package net.yegong.matrix;

import org.netlib.lapack.Spotrf;
import org.netlib.util.intW;

/**
 * @author cooper
 */
public class CholeskyDecomposition {

	public CholeskyDecomposition(SymmetricMatrix mat) {
		this.mat = mat;
	}

	public void calc() {
		int n = mat.nRow;
		float[] a = MatrixHelper.copy(mat.array);
		intW info = new intW(0);
		Spotrf.spotrf("U", n, a, 0, n, info);
		if (info.val != 0)
			return;
		r = new SquareMatrix(a, n, Matrix.FORTRAN);
		r.reserveUpperTriangle();
	}

	public SquareMatrix getR() {
		if (r == null)
			calc();
		return r;
	}

	protected SquareMatrix r;
	protected SymmetricMatrix mat;
}
